package com.hs.dao.DaoImpl;

import com.hs.dao.CourierStationDao;
import com.hs.entity.CourierStation;
import com.hs.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CourierStationDaoImpl implements CourierStationDao {
    @Override
    public int insert(CourierStation courierStation) {
        String sql = "insert into courierstation values (null,?,?,?,?,?,?,?,?,?,?,?)";
        Object obj[] = {
                courierStation.getCourierStationName(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationDistrict(),
                new Date(),
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationCommunityId(),
                courierStation.getCourierStationCommunityName(),
                courierStation.getCourierStationOrderCount(),
                courierStation.getCourierStationSort(),
                courierStation.getCourierStationStatus()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int update(CourierStation courierStation) {
        String sql = "update courierstation set courierStationName = ?,courierStationProvince = ?,courierStationCity = ?,courierStationDistrict = ?, courierStationAddress = ?, courierStationCommunityName = ? where courierStationId = ?";
        Object obj[] = {
                courierStation.getCourierStationName(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationDistrict(),
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationCommunityName(),
                courierStation.getCourierStationId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<CourierStation> selectAll(Map<String, String> map) {
        List<CourierStation> list = new ArrayList<>();
        String sql = "select * from courierstation where 1=1 ";
        List params = new ArrayList();
        String courierStationName = map.get("courierStationName");
        if (courierStationName != null && !"".equals(courierStationName)) {
            sql += " and courierStationName like ?";
            params.add("%" + courierStationName + "%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String communityName = map.get("communityName");
        if (communityName!=null&&!"".equals(communityName)){
            sql += " and courierStationCommunityName like ?";
            params.add(communityName);
        }
        //判断是否分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,?";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }

        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                //组成驿站对象
                CourierStation courierStation = new CourierStation(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getDate(6),
                        res.getString(7),
                        res.getInt(8),
                        res.getString(9),
                        res.getInt(10),
                        res.getInt(11),
                        res.getInt(12)
                        );
                list.add(courierStation);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        String sql = "select count(1) from courierStation where 1=1 ";
        List params = new ArrayList();
        String courierStationName = map.get("courierStationName");
        if (courierStationName!=null&&!"".equals(courierStationName)){
            sql += " and courierStationName like ?";
            params.add(courierStationName);
        }
        String startTime = map.get("startTime");
        if (startTime!=null&&!"".equals(startTime)){
            sql += " and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String communityName = map.get("communityName");
        if (communityName!=null&&!"".equals(communityName)){
            sql += " and courierStationCommunityName like ?";
            params.add(communityName);
        }

        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                count = res.getInt(1);

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return count;
    }

    @Override
    public int changeStatus(String courierStationStatus, Integer courierStationId) {
        String sql = "update courierStation set courierStationStatus = ? where courierStationId = ?";
        int num = 0;
        if ("1".equals(courierStationStatus)) {
            Object[] obj = {"0",courierStationId};
            num = JDBCDruidUtil.update(sql, obj);
        } else if ("0".equals(courierStationStatus)) {
            Object[] obj = {"1",courierStationId};
            num = JDBCDruidUtil.update(sql, obj);
        }
        return  num;
    }
}
